local skel = fk.CreateSkill {
  name = "lb_zero__chihai",
}

Fk:loadTranslationTable {
  ["lb_zero__chihai"] = "炽骇",
  [":lb_zero__chihai"] = "每轮开始时，你可以重铸一半手牌（向上取整），直到手牌中有【酒】或<font color=#CC3131><b>♥</b></font>牌，然后体力值唯一最大的其他角色再重铸你一半手牌，直到手牌中没有【酒】或没有<font color=#CC3131><b>♥</b></font>牌。" ..
      "你手牌中在三或更多次重铸后仍保留的牌视为火【杀】，在不因使用而失去时，将之当火【杀】并摸一张牌。",

  ["#lb_zero__chihai"] = "炽骇：你可以重铸一半手牌（%arg张），直到手牌中有【酒】或<font color=#CC3131><b>♥</b></font>牌",
  ["#lb_zero__chihai-recast"] = "炽骇：请重铸一半手牌（%arg张），直到手牌中有【酒】或<font color=#CC3131><b>♥</b></font>牌",
  ["#lb_zero__chihai-choose"] = "炽骇：请重铸 %src 一半手牌（%arg张）",
  ["#lb_zero__chihai-use"] = "炽骇：请使用火【杀】并摸一张牌",

  --["@@lb_zero__chihai-inhand"] = "炽骇",
  ["@lb_zero__chihai-inhand"] = "炽骇", --宝宝标记

  ["$lb_zero__chihai1"] = "要来一杯燃油饮吗？",
  ["$lb_zero__chihai2"] = "一起燃一下吧！",
  ["$lb_zero__chihai3"] = "狂欢之夜，马上开场！",
  ["$lb_zero__chihai4"] = "哟~一起开心一下吧！",
}

skel:addEffect(fk.RoundStart, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and not player:isKongcheng()
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local num = math.floor((player:getHandcardNum() / 2) + 0.5)
    local cards = room:askToCards(player, {
      min_num = num,
      max_num = num,
      skill_name = skel.name,
      cancelable = true,
      include_equip = false,
      prompt = "#lb_zero__chihai:::" .. num,
    })
    if #cards > 0 then
      event:setCostData(self, { cards = cards })
      return true
    end
  end,
  --[[
  on_use = function(self, event, target, player, data)
    local room = player.room
    local recast_list = {}
    for _, id in ipairs(player:getCardIds("h")) do
      recast_list[id] = recast_list[id] or 0
    end
    room:recastCard(event:getCostData(self).cards, player, skel.name)
    for _, id in ipairs(player:getCardIds("h")) do
      if table.contains(event:getCostData(self).cards, id) or not recast_list[id] then
        recast_list[id] = 0
      else
        recast_list[id] = 1
      end
    end
    while not player:isKongcheng() and not table.find(player:getCardIds("h"), function(id)
        local card = Fk:getCardById(id, false)
        return card.name == "analeptic" or card.suit == Card.Heart
      end) do
      for _, id in ipairs(player:getCardIds("h")) do
        recast_list[id] = recast_list[id] or 0
      end
      local num = math.floor((player:getHandcardNum() / 2) + 0.5)
      local cards = room:askToCards(player, {
        min_num = num,
        max_num = num,
        skill_name = skel.name,
        cancelable = false,
        include_equip = false,
        prompt = "#lb_zero__chihai-recast:::" .. num,
      })
      if #cards > 0 then
        room:recastCard(cards, player, skel.name)
        for id, _ in pairs(recast_list) do
          if table.contains(cards, id) then
            recast_list[id] = 0
          else
            recast_list[id] = recast_list[id] + 1
            if recast_list[id] > 2 and table.contains(player:getCardIds("h"), id) then
              room:setCardMark(Fk:getCardById(id), "@@lb_zero__chihai-inhand", 1)
            end
          end
        end
      end
    end
    local to = table.find(room.alive_players, function(to)
      return not table.find(room:getOtherPlayers(to), function(t)
        return t.hp >= to.hp
      end)
    end)
    while to and to ~= player and not player:isKongcheng() and table.find(player:getCardIds("h"), function(id)
        local card = Fk:getCardById(id, false)
        return card.name == "analeptic" or card.suit == Card.Heart
      end) do
      for _, id in ipairs(player:getCardIds("h")) do
        recast_list[id] = recast_list[id] or 0
      end
      local num = math.floor((player:getHandcardNum() / 2) + 0.5)
      local cards = room:askToChooseCards(to, {
        target = player,
        flag = "h",
        skill_name = skel.name,
        min = num,
        max = num,
        prompt = "#lb_zero__chihai-choose:" .. player.id .. "::" .. num,
      })
      if #cards > 0 then
        room:recastCard(cards, player, skel.name)
        for id, _ in pairs(recast_list) do
          if table.contains(cards, id) then
            recast_list[id] = 0
          else
            recast_list[id] = recast_list[id] + 1
            if recast_list[id] > 2 and table.contains(player:getCardIds("h"), id) then
              room:setCardMark(Fk:getCardById(id), "@@lb_zero__chihai-inhand", 1)
            end
          end
        end
      end
      to = table.find(room.alive_players, function(alive_p)
        return not table.find(room:getOtherPlayers(alive_p), function(t)
          return t.hp >= alive_p.hp
        end)
      end)
    end
  end,
  --]]
  on_use = function(self, event, target, player, data)
    local room = player.room
    local hand_cards = player:getCardIds("h")
    room:recastCard(event:getCostData(self).cards, player, skel.name)
    for _, id in ipairs(hand_cards) do
      if not table.contains(event:getCostData(self).cards, id) and table.contains(player:getCardIds("h"), id) then
        room:addCardMark(Fk:getCardById(id), "@lb_zero__chihai-inhand")
      end
    end
    while not player:isKongcheng() and not table.find(player:getCardIds("h"), function(id)
        local card = Fk:getCardById(id, false)
        return card.name == "analeptic" or card.suit == Card.Heart
      end) do
      hand_cards = player:getCardIds("h")
      local num = math.floor((player:getHandcardNum() / 2) + 0.5)
      local cards = room:askToCards(player, {
        min_num = num,
        max_num = num,
        skill_name = skel.name,
        cancelable = false,
        include_equip = false,
        prompt = "#lb_zero__chihai-recast:::" .. num,
      })
      if #cards > 0 then
        room:recastCard(cards, player, skel.name)
        for _, id in ipairs(hand_cards) do
          if not table.contains(cards, id) and table.contains(player:getCardIds("h"), id) then
            room:addCardMark(Fk:getCardById(id), "@lb_zero__chihai-inhand")
          end
        end
      end
    end
    local to = table.find(room.alive_players, function(to)
      return not table.find(room:getOtherPlayers(to), function(t)
        return t.hp >= to.hp
      end)
    end)
    while to and to ~= player and not player:isKongcheng() do
      hand_cards = player:getCardIds("h")
      local num = math.floor((player:getHandcardNum() / 2) + 0.5)
      local cards = room:askToChooseCards(to, {
        target = player,
        flag = "h",
        skill_name = skel.name,
        min = num,
        max = num,
        prompt = "#lb_zero__chihai-choose:" .. player.id .. "::" .. num,
      })
      if #cards > 0 then
        room:recastCard(cards, player, skel.name)
        for _, id in ipairs(hand_cards) do
          if not table.contains(cards, id) and table.contains(player:getCardIds("h"), id) then
            room:addCardMark(Fk:getCardById(id), "@lb_zero__chihai-inhand")
          end
        end
      end
      if not table.find(player:getCardIds("h"), function(id)
        local card = Fk:getCardById(id, false)
        return card.name == "analeptic"
      end) or not table.find(player:getCardIds("h"), function(id)
        local card = Fk:getCardById(id, false)
        return card.suit == Card.Heart
      end) then
        break
      end
      to = table.find(room.alive_players, function(alive_p)
        return not table.find(room:getOtherPlayers(alive_p), function(t)
          return t.hp >= alive_p.hp
        end)
      end)
    end
  end,
})

skel:addEffect(fk.BeforeCardsMove, {
  anim_type = "offensive",
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(skel.name) then
      for _, move in ipairs(data) do
        if move.from == player and move.moveReason ~= fk.ReasonUse and table.find(move.moveInfo, function(info)
              return Fk:getCardById(info.cardId):getMark("@lb_zero__chihai-inhand") > 2
            end) then
          return true
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local ids, cancel_ids = {}, {}
    for _, move in ipairs(data) do
      if move.from == player and move.moveReason ~= fk.ReasonUse then
        for _, info in ipairs(move.moveInfo) do
          if Fk:getCardById(info.cardId):getMark("@lb_zero__chihai-inhand") > 2 then
            table.insertIfNeed(ids, info.cardId)
          end
        end
      end
    end
    --[[
    while #ids > 0 and player:isAlive() do
      local use = room:askToPlayCard(player, {
        pattern = tostring(Exppattern { id = ids }),
        skill_name = skel.name,
        cancelable = false,
        prompt = "#lb_zero__chihai-use",
        skip = true,
      })
      if use then
        table.removeOne(ids, use.card:getEffectiveId())
        table.insert(cancel_ids, use.card:getEffectiveId())
        --use.extraUse = true
        room:useCard(use)
        if player:isAlive() then
          player:drawCards(1, skel.name)
        end
      else
        break
      end
    end
    if #cancel_ids > 0 then
      room:cancelMove(data, cancel_ids)
    end
    --]]


    local use = room:askToUseVirtualCard(player, {
      name = "fire__slash",
      subcards = ids,
      skill_name = skel.name,
      cancelable = false,
      prompt = "#lb_zero__chihai-use",
      skip = true,
    })
    if use then
      room:cancelMove(data, ids)
      --use.extraUse = true
      room:useCard(use)
      if player:isAlive() then
        player:drawCards(1, skel.name)
      end
    end
  end,
})


skel:addEffect("filter", {
  mute = true,
  card_filter = function(self, card, player, isJudgeEvent)
    if player:hasSkill(skel.name) and card:getMark("@lb_zero__chihai-inhand") > 2 then
      return true
    end
  end,
  view_as = function(self, player, card)
    local slash = Fk:cloneCard("fire__slash", card.suit, card.number)
    slash.skillName = skel.name
    return slash
  end,
})

return skel
